/**
 * Created by lwy on 2017/7/23.
 */

var page = 1;
var id = '';
var userName = '',
    stateValue = '',
    realName = '';

function doAjax(){
    $.ajax({
        url:'/crm/agentSalesmanC/getUnderlingSalesman',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            page:page,
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.length;i++){
                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;">'+data.datas[i].id+'</td></a>'+
                        '<td class="center ">'+data.datas[i].userAccount+'</td>'+
                        '<td class="center ">'+data.datas[i].realName+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].phone+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].shopCount+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].salesmanPerformance+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].userState+'</td>'+
                        '<td class="sorting_1 withdrawId" data-toggle="modal" data-target="#myModal4" style="text-decoration: underline;cursor: pointer;" id="'+data.datas[i].id+'">删除</td>'+
                        '<td class="sorting_1 initialize" style="text-decoration: underline;cursor: pointer;" initialize="'+data.datas[i].id+'">初始化</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }

                //初始化密码
                $('.initialize').unbind('click').click(function(){
                    $.ajax({
                        url:'/crm/userC/initCRMPassword',
                        dataType:'json',
                        data:{
                            id:$(this).attr('initialize'),
                            token:localStorage.getItem('crmtoken'),
                        },
                        type:'post',
                        success:function(data){
                            if(data.code==-1){
                                window.parent.location.href="/crm/login.jsp"
                            }
                            if(data.code==0){
                                alert(data.msg)
                                return;
                            }
                            if(data.code==1){
                                alert('初始化成功');
                            }

                        }
                    })
                })

                //id处理
                $('.withdrawId').click(function(){
                    id = $(this).attr('id');
                    $.ajax({
                        url:'/crm/agentSalesmanC/getUnderlingSalesman',
                        dataType:'json',
                        data:{
                            token:localStorage.getItem('crmtoken'),
                            state:1,
                        },
                        type:'post',
                        success:function(data){
                            $('#replace').empty();
                            $('<option value="">请选择接替业务员</option>').appendTo($('#replace'));
                            for(var i=0;i<data.datas.length;i++){
                                $('<option value="'+data.datas[i].id+'">'+data.datas[i].realName+'</option>').appendTo($('#replace'));
                            }
                        }
                    })
                })
                $(".pagination").createPage({
                    totalPage:data.totalPage,
                    currPage:data.pageNow,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}

$(function(){
    doAjax();
})

//查询
$('#refer').click(function(){
    userName = $('#salesmanName').val();
    realName = $('#salesmanrealName').val();
    stateValue = $('#state').val();
    page=1;
    doAjax();
})

//检验登录账号
$('#agencyloginName').blur(function(){
    var username = $('#agencyloginName').val();
    checkUser(username);
})

//检验手机号
$('#agencyphone').blur(function(){
    var mobile = $('#agencyphone').val();
    checkMobile(mobile);
})

//添加
$('#save').click(function(){
    var username2 = $('#agencyloginName').val();
    var mobile2 = $('#agencyphone').val();
    if(!checkUser(username2)){
        return;
    }
    if(!checkMobile(mobile2)){
        return;
    }

    $.ajax({
        url:'/crm/agentSalesmanC/addSalesman',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            userName:$('#agencyloginName').val(),
            realName:$('#agencyuserName').val(),
            phone:$('#agencyphone').val(),
            bilv:$('#bliv').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('添加成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#myModal5 input').val('');
                page=1;
                doAjax();
            }
        }
    })
})

//无效
$('#save2').click(function(){
    $.ajax({
        url:'/crm/agentSalesmanC/agentInvalidSalesman',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            id:id,
            successorId:$('#replace').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('无效成功');
                $('#myModal4').removeClass('fade in');
                $('#myModal4').addClass('fade');
                $('#myModal4').attr('aria-hidden',true);
                $('#myModal4').css('display','none');
                $('.modal-backdrop').css('display','none');
                page=1;
                doAjax();
            }
        }
    })
})


//检验登录账号
$('#agencyloginNameSum').blur(function(){
    var username2 = $('#agencyloginNameSum').val();
    checkUser2(username2);
})

//检验手机号
$('#agencyphoneSum').blur(function(){
    var mobile2 = $('#agencyphoneSum').val();
    checkMobile2(mobile2);
})

//登录账号是否包含字母
function checkUser2(username)
{
    var user=username;
    var r2 = /[a-zA-Z]/i;
    //如果为空
    if(user==''){
        $('#userTips2').show();
        $('#userTips2').html('登录账号不能为空');
        return false;
    }
    //如果不包含字母
    if(!r2.test(user))
    {
        $('#userTips2').show();
        $('#userTips2').html('登录账号必须包含字母');
        return false;
    }

    $('#userTips2').hide();
    return true;
}

//检查手机号
function checkMobile2(Mobile){
    var sMobile = Mobile;
    if(!(/^1(3|4|5|7|8)\d{9}$/.test(sMobile))){
        $('#phoneTips2').show();
        $('#phoneTips2').html('手机号格式不正确');
        return false;
    }
    $('#phoneTips2').hide();
    return true;
}

//添加业务主管
$('#save3').click(function(){
    var username3 = $('#agencyloginNameSum').val();
    var mobile3 = $('#agencyphoneSum').val();
    if(!checkUser(username3)){
        return;
    }
    if(!checkMobile(mobile3)){
        return;
    }

    $.ajax({
        url:'/crm/agentSalesmanC/addSalesmanAdmin',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            userName:$('#agencyloginNameSum').val(),
            realName:$('#agencyuserNameSum').val(),
            phone:$('#agencyphoneSum').val(),
            bilv:$('#blivSum').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('添加成功');
                $('#myModal3').removeClass('fade in');
                $('#myModal3').addClass('fade');
                $('#myModal3').attr('aria-hidden',true);
                $('#myModal3').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#myModal3 input').val('');
                page=1;
                doAjax();
            }
        }
    })
})
